package com.xyyl.patient.common.utils;

import java.util.Calendar;
import java.util.Date;

import com.xyyl.patient.core.util.DateUtils;

/**
 * @projectName:xyyl-patient-web
 * @ClassName: CalculateAgeUtils
 * @Description: 计算年龄tools
 * @author chen
 * @date 2018年1月10日
 *
*/

public class CalculateAgeUtils {

	public static int getAge(String birthDay) {

		Date birthDate = DateUtils.parseDate(birthDay);
		Calendar cal = Calendar.getInstance();
		if (cal.before(birthDate)) {
			throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthDate);

		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth)
					age--;
			} else {
				age--;
			}
		}
		return age;
	}
	
}
